package com.boot.web.serviceImpl;

import com.boot.web.dao.MenuPowerDao;
import com.boot.web.dao.RolePowerDao;
import com.boot.web.dto.ResourcesDTO;
import com.boot.web.entity.MenuPower;
import com.boot.web.entity.RolePower;
import com.boot.web.service.MenuPowerService;
import com.boot.web.vo.MenuPowerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.base.serviceImpl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单权限信息业务层接口实现
 * @author 【zhouJie】
 * @date 2019-12-05 17:31:35
 */
@Service
public class MenuPowerServiceImpl extends BaseServiceImpl<MenuPower> implements MenuPowerService {

    @Autowired
    private MenuPowerDao menuPowerDao;

    @Autowired
    private RolePowerDao rolePowerDao;

    @Override
    public List<MenuPowerVO> getMenuPowerList() {
        List<MenuPowerVO> menuList = new ArrayList<>();
        List<MenuPowerVO> onePowerList = menuPowerDao.queryAllPowerByOneLevel(); // TODO 第一层
        int oneSize = onePowerList.size();
        List<MenuPowerVO> twoPowerList = menuPowerDao.queryAllPowerByTwoLevel(); // TODO 第二层
        int twoSize = twoPowerList.size();
        List<MenuPowerVO> threePowerList = menuPowerDao.queryAllPowerByThreeLevel(); // TODO 第三层
        int threeSize = threePowerList.size();
        List<MenuPowerVO> fourPowerList = menuPowerDao.queryAllPowerByFourLevel(); // TODO 第四层
        int fourSize = fourPowerList.size();
        if (oneSize > 0) {
            List<MenuPowerVO> oneLevelList = new ArrayList<>();
            for (MenuPowerVO oneP : onePowerList) {
                if (oneP.getType() == 0 && oneP.getLevel() == 0 && oneP.getParentId() == null) {
                    if (twoSize > 0) {
                        List<MenuPowerVO> twoLevelList = new ArrayList<>();
                        for (MenuPowerVO twoP : twoPowerList) {
                            if (twoP.getType() == 0 && twoP.getLevel() == 1 && twoP.getParentId().equals(oneP.getId())) {
                                twoLevelList.add(twoP);
                                oneP.setChildren(twoLevelList);
                                if (threeSize > 0) {
                                    List<MenuPowerVO> threeLevelList = new ArrayList<>();
                                    for (MenuPowerVO threeP : threePowerList) {
                                        if (threeP.getType() == 0 && threeP.getLevel() == 2 && threeP.getParentId().equals(twoP.getId())) {
                                            threeLevelList.add(threeP);
                                            twoP.setChildren(threeLevelList);
                                            if (fourSize > 0) {
                                                List<MenuPowerVO> fourLevelList = new ArrayList<>();
                                                for (MenuPowerVO fourP : fourPowerList) {
                                                    if (fourP.getType() == 1 && fourP.getLevel() == 3 && fourP.getParentId().equals(threeP.getId())) {
                                                        fourLevelList.add(fourP);
                                                        threeP.setChildren(fourLevelList);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    oneLevelList.add(oneP);
                }
            }
            menuList = oneLevelList;
        }
        return menuList;
    }

    @Override
    public boolean batchDeleteMenuPower(Integer[] ids) {
        rolePowerDao.batchDeleteByPowerId(ids);
        menuPowerDao.batchDelete(ids);
        return true;
    }

    @Override
    public Map<String, Object> getResources(ResourcesDTO resourcesDTO) {
        Map<String, Object> map = new HashMap<>();
        List<MenuPower> menuPowerList = menuPowerDao.queryMenuPowerByParentId(resourcesDTO.getMenuPowerId());
        if(menuPowerList.size() > 0){
            for(int i = 0; i < menuPowerList.size(); i++){
                MenuPower mp = menuPowerList.get(i);
                if (resourcesDTO.getRoleIds().length > 0) {
                    for (int j = 0; j < resourcesDTO.getRoleIds().length; j++) {
                        Integer roleId = resourcesDTO.getRoleIds()[j];
                        RolePower rp = rolePowerDao.queryRolePowerByIds(roleId, mp.getId());
                        if(rp != null){
                            map.put(mp.getName(), 1); //显示
                        }else{
                            map.put(mp.getName(), 2); //不显示
                        }
                    }
                }
            }
        }
        return map;
    }
}
